package cn.com.homeyoda.aimate.aihealth.ui.health.frragment;

import android.content.Context;
import android.content.Intent;
import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.github.AAChartModel.AAChartCore.AAChartCreator.AAChartModel;
import com.github.AAChartModel.AAChartCore.AAChartCreator.AAChartView;
import com.github.AAChartModel.AAChartCore.AAChartCreator.AASeriesElement;
import com.github.AAChartModel.AAChartCore.AAChartEnum.AAChartLineDashStyleType;
import com.github.AAChartModel.AAChartCore.AAChartEnum.AAChartType;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAChart;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AALabels;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAMarker;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAOptions;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAPlotLinesElement;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAStyle;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AATitle;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAXAxis;
import com.github.AAChartModel.AAChartCore.AAOptionsModel.AAYAxis;
import com.github.AAChartModel.AAChartCore.AATools.AAGradientColor;
import com.github.AAChartModel.AAChartCore.AATools.AALinearGradientDirection;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.bean.ChronicDiseaseItemBean;
import cn.com.homeyoda.aimate.aihealth.bean.InspectionScheduleTypeBean;
import cn.com.homeyoda.aimate.aihealth.bean.PopularListBean;
import cn.com.homeyoda.aimate.aihealth.manager.UserInfoManager;
import cn.com.homeyoda.aimate.aihealth.ui.detail.DetailActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.ChronicDiseaseActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.ChronicDiseaseAddActivity;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.ChroicDiseaseTitleAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.DailyKepuAdapter;
import cn.com.homeyoda.aimate.aihealth.ui.health.adapter.DiseaseSelectAdapter;
import cn.com.homeyoda.aimate.aihealth.views.MemberChangeDialog;
import cn.com.homeyoda.aimate.aihealth.views.SpaceItemDecoration;

/**
 * 慢病管理
 */
public class ChronicDiseaseAddFragment extends Fragment {
    private ChronicDiseaseActivity mActivity;
    private boolean initViewObservable = false;
    private View mRootView;

    private DiseaseSelectAdapter selectAdapter;
    private RecyclerView rvDiseaseSelect;
    private View btnOk;


    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        mActivity = (ChronicDiseaseActivity) context;

        if (!initViewObservable) {
            initViewObservable();
        }
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        if (mRootView == null) {
            mRootView = inflater.inflate(R.layout.fragment_chronic_disease_add, container, false);
            initView();
        }


        return mRootView;
    }

    private void initView() {
        rvDiseaseSelect = mRootView.findViewById(R.id.rv_disease_select);
        btnOk = mRootView.findViewById(R.id.btn_ok);

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String dictCodes = "";
                UserInfoManager.getInstance().chronicDiseaseMap.clear();
                for (ChronicDiseaseItemBean.DataBean bean : mActivity.getViewModel().chroincDiseaseSelectList.getValue()) {
                    if (bean.isSelect()) {
                        UserInfoManager.getInstance().chronicDiseaseMap.put(bean.getDictCode(), bean);
                        dictCodes += bean.getDictCode() + ",";
                    } else {
                        if (UserInfoManager.getInstance().chronicDiseaseMap.containsKey(bean.getDictCode())) {
                            UserInfoManager.getInstance().chronicDiseaseMap.remove(bean.getDictCode());
                        }
                    }
                }

                mActivity.getViewModel().initChronicDisease();

                mActivity.getViewModel().attentionChronicAdd(dictCodes);
                mActivity.changeChronicDiseaseFragment();
            }
        });

        initRecyclerView();
    }


    private void initRecyclerView() {
        List<ChronicDiseaseItemBean.DataBean> data = new ArrayList<>();
        if (mActivity.getViewModel().chroincDiseaseSelectList.getValue() != null){
            data = mActivity.getViewModel().chroincDiseaseSelectList.getValue();
        }

        selectAdapter = new DiseaseSelectAdapter(data, mActivity);
        rvDiseaseSelect.setLayoutManager(new GridLayoutManager(mActivity, 3));
        rvDiseaseSelect.addItemDecoration(new SpaceItemDecoration(0, 0, 25, 20));
        rvDiseaseSelect.setAdapter(selectAdapter);

        selectAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ChronicDiseaseItemBean.DataBean bean = selectAdapter.getData().get(position);
                bean.setSelect(!bean.isSelect());
                selectAdapter.notifyDataSetChanged();
            }
        });

    }


    public void initViewObservable() {

        if (mActivity == null) {
            return;
        }

        initViewObservable = true;

        mActivity.getViewModel().chroincDiseaseSelectList.observe(mActivity, chroincDiseaseSelectList -> {
            if (selectAdapter != null) {
                selectAdapter.setNewData(chroincDiseaseSelectList);
            }
        });

    }


}
